import bpy; 


def find( data ):
	for obj in bpy.data.objects:
		if obj.data == data:
			return obj
	raise

def renderActions(selected, action, camera, path):
    if action.name.startswith("_"):
        return

    bpy.data.scenes[0].render.filepath = path + "_" + camera.name + "_" + action.name + "\\"
    if selected.animation_data:
        selected.animation_data.action = action
        bpy.ops.render.render(animation=True)
    else:
        bpy.ops.render.render(write_still=True)

def renderAll( selected ):
	try:
		path = bpy.data.scenes[0].render.filepath
		if path.find("tiles") > 0:
			bpy.data.scenes[0].render.resolution_x = 50
			bpy.data.scenes[0].render.resolution_y = 50
			bpy.data.scenes[0].render.resolution_percentage = 100

		for c in bpy.data.cameras:
			bpy.context.scene.camera = find(c)
			cname = bpy.context.scene.camera.name
			bpy.data.scenes[0].render.filepath = path + "_" + c.name
			bpy.context.scene.frame_current = 0
			bpy.ops.render.render(write_still=True)
			if selected:
				for a in bpy.data.actions:
					renderActions(selected, a, c, path)

			bpy.data.scenes[0].render.filepath = path
	except:
		bpy.data.scenes[0].render.filepath = path
		raise

for o in bpy.data.objects:
	if o.animation_data:
		renderAll( o )
		break

renderAll( 0 )
